home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / FML.C < prev    next >
Text File  |  1991-09-23  |  5KB  |  198 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)fml.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #include <stdio.h>
  14. #ifdef AC_STDLIB
  15. #include <stdlib.h>
  16. #endif
  17. #ifdef AC_STRING
  18. #include <string.h>
  19. #endif
  20.  
  21. /* constants */
  22. #define NUL    ('\0')    /* nul character */
  23.  
  24. /* macros */
  25. #define min(a,b)    ((a) < (b) ? (a) : (b))
  26.  
  27. /*man---------------------------------------------------------------------------
  28. NAME
  29.      fmltolfm - convert name from fml to lfm format
  30.  
  31. SYNOPSIS
  32.      int fmltolfm(t, s, n)
  33.      char *t;
  34.      const char *s;
  35.      size_t n;
  36.  
  37. DESCRIPTION
  38.      The fmltolfm function converts a name from the format "first
  39.      middle last" to "last first middle".  s points to the
  40.      nul-terminated source string in fml format.  t points to the
  41.      target string to receive the converted name.  t and s may point
  42.      to the same string.  There may be more than one middle name, or
  43.      the middle name may be absent.  All leading and trailing spaces
  44.      must be removed from the source string before calling fmltolfm.
  45.      At most n characters are copied.  The target will be padded with
  46.      nuls if the name has fewer than n characters.
  47.  
  48.      fmltolfm will fail if one or more of the following is true:
  49.  
  50.      [EINVAL]       t or s is the NULL pointer.
  51.  
  52. SEE ALSO
  53.      lfmtofml.
  54.  
  55. DIAGNOSTICS
  56.      Upon successful completion, a value of 0 is returned.  Otherwise,
  57.      a value of -1 is returned, and errno set to indicate the error.
  58.  
  59. ------------------------------------------------------------------------------*/
  60. #ifdef AC_PROTO
  61. int fmltolfm(char *t, const char *s, size_t n)
  62. #else
  63. int fmltolfm(t, s, n)
  64. char *t;
  65. const char *s;
  66. size_t n;
  67. #endif
  68. {
  69.     size_t    len    = 0;        /* string length */
  70.     char *    p    = NULL;
  71.     char *    ts    = NULL;        /* temporary string */
  72.  
  73.     /* validate arguments */
  74.     if (t == NULL || s == NULL) {
  75.         errno = EINVAL;
  76.         return -1;
  77.     }
  78.  
  79.     /* get length of name */
  80.     len = strlen(s);
  81.  
  82.     /* find beginning of last name */
  83.     p = strrchr(s, ' ');
  84.     if (p == NULL) {
  85.         strncpy(t, s, n);
  86.         return 0;
  87.     }
  88.  
  89.     /* create temporary string */
  90.     ts = (char *)calloc(n + 1, (size_t)1);
  91.     if (ts == NULL) {
  92.         errno = ENOMEM;
  93.         return -1;
  94.     }
  95.  
  96.     /* perform conversion */
  97.     strncpy(ts, p + 1, n);            /* copy last name */
  98.     ts[n] = NUL;
  99.     strncat(ts, " ", n - strlen(ts));    /* add space after last name */
  100.     ts[n] = NUL;
  101.     strncat(ts, s, n - strlen(ts));        /* copy beginning of name */
  102.     if (len < n) {        /* remove space that marked last name */
  103.         ts[len] = NUL;
  104.     }
  105.     strncpy(t, ts, n);            /* copy converted string to t */
  106.  
  107.     /* free temporary string */
  108.     free(ts);
  109.  
  110.     return 0;
  111. }
  112.  
  113. /*man---------------------------------------------------------------------------
  114. NAME
  115.      lfmtofml - convert name from lfm to fml format
  116.  
  117. SYNOPSIS
  118.      int lfmtofml(t, s, n)
  119.      char *t;
  120.      const char *s;
  121.      size_t n;
  122.  
  123. DESCRIPTION
  124.      The lfmtofml function converts a name from the format "last
  125.      first middle" to "first middle last".  s points to the
  126.      nul-terminated source string in lfm format.  t points to the
  127.      target string to receive the converted name.  t and s may point
  128.      to the same string.  There may be more than one middle name, or
  129.      the middle name may be absent.  All leading and trailing spaces
  130.      must be removed from the source string before calling fmltolfm.
  131.      At most n characters are copied.  The target will be padded with
  132.      nuls if the name has fewer than n characters.
  133.  
  134.      lfmtofml will fail if one or more of the following is true:
  135.  
  136.      [EINVAL]       t or s is the NULL pointer.
  137.  
  138. SEE ALSO
  139.      fmltolfm.
  140.  
  141. DIAGNOSTICS
  142.      Upon successful completion, a value of 0 is returned.  Otherwise,
  143.      a value of -1 is returned, and errno set to indicate the error.
  144.  
  145. ------------------------------------------------------------------------------*/
  146. #ifdef AC_PROTO
  147. int lfmtofml(char *t, const char *s, size_t n)
  148. #else
  149. int lfmtofml(t, s, n)
  150. char *t;
  151. const char *s;
  152. size_t n;
  153. #endif
  154. {
  155.     size_t    len    = 0;        /* string length */
  156.     char *    p    = NULL;
  157.     char *    ts    = NULL;        /* temporary string */
  158.  
  159.     /* validate arguments */
  160.     if (t == NULL || s == NULL) {
  161.         errno = EINVAL;
  162.         return -1;
  163.     }
  164.  
  165.     /* get length of name */
  166.     len = strlen(s);
  167.  
  168.     /* find end of last name */
  169.     p = strchr(s, ' ');
  170.     if (p == NULL) {
  171.         strncpy(t, s, n);
  172.         return 0;
  173.     }
  174.  
  175.     /* create temporary string */
  176.     ts = (char *)calloc(n + 1, (size_t)1);
  177.     if (ts == NULL) {
  178.         errno = ENOMEM;
  179.         return -1;
  180.     }
  181.  
  182.     /* perform conversion */
  183.     strncpy(ts, p + 1, n);            /* copy beginning of name */
  184.     ts[n] = NUL;
  185.     strncat(ts, " ", n - strlen(ts));    /* add space before last name */
  186.     ts[n] = NUL;
  187.     strncat(ts, s, n - strlen(ts));        /* copy last name */
  188.     if (len < n) {        /* remove space that marked first name */
  189.         ts[len] = NUL;
  190.     }
  191.     strncpy(t, ts, n);            /* copy converted string to t */
  192.  
  193.     /* free temporary string */
  194.     free(ts);
  195.  
  196.     return 0;
  197. }
  198.